# -*- coding: utf-8 -*-



## ------------------------------------------------------------
## Copyright 2010 Pierre-Antoine Delsart
## 
## This file is part of LeVin.
## 
## LeVin is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## LeVin is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with LeVin.  If not, see <http://www.gnu.org/licenses/>.
## ---------------------------------------------------------------


from qtcommon import connect
from PythonQt import QtCore,QtGui

import components

class TabComponent(components.Component):
    """ The component base class for all component representing a tab in the right side of the
    main view.
    Each tab is implemented by a class inheriting this class and defining update() and setupTab() function
    """
    tabWidget = None
    def __init__(self, tabId=0):
        self.tabId = tabId
        self.current_row = None

    def setup(self):
        self.setupTabWidget()
        self.setupTab(self.tabWidget)
        self.tabWidget.setCurrentIndex(0)


    def setupEditButtonBox(self):
        """ Put the common editWineButtonBox at the bottom, below tabs."""
        self.verticalLayoutDesc.addWidget(self.mainWindow.editWineButtonBox.editWine_buttonBox)
        
    def setupTabWidget(self):
        if TabComponent.tabWidget is not None:
            return
        TabComponent.tabWidget = QtGui.QTabWidget(self.mainWindow.rightSideWid)
        self.tabWidget.setObjectName("tabWidget")
        self.mainWindow.rightSideLayout.addWidget( self.tabWidget )
        
    def countryRowSelected(self,row):
        self.tabWidget.setVisible(False)
    def regionRowSelected(self,row):
        self.tabWidget.setVisible(False)

    def wineRowSelected(self,row):
        self.tabWidget.setVisible(True)
        if self.tabId == self.tabWidget.currentIndex():
            self.update(row.bottle)
            self.current_row = row

    def tabChanged(self,i):
        if i != self.tabId :
            return
        self.setupEditButtonBox()
        row = self.mainWindow.currentRow()
        if self.current_row != row:
            self.update(row.bottle)
            self.current_row = row

    def connect(self):
        mainWindow = self.mainWindow        
        connect(self.tabWidget , QtCore.SIGNAL("currentChanged(int)") , self.tabChanged)
